var refreshGirdData; // 更新数据
var companyId = '';
var ztId = '';
var departmentId = '';
var userdefault = {};
var params = {};
var bootstrap = function ($, learun) {

    var pageWidth = 1024;
    var colNames = [];

    "use strict";
    var page = {
        init: function () {
            userdefault = learun.clientdata.get(['usersetting']);
            pageWidth = ($('.lr-layout-grid').width());

            page.bind();
        },
        bind: function () {
            $('#multiple_condition_query').lrMultipleQuery(function (queryJson) {
                // 调用后台查询
                // queryJson 查询条件
                page.search(queryJson);

            }, 200, 480);
            // 搜索框初始化
            $('#txt_Keyword').on("keypress", function (e) {
                if (event.keyCode == "13") {
                    var $this = $(this);
                    var keyword = $this.val();
                    page.search({keyword: keyword});
                }
            });
            // 统计方式
            $('#groupCode').lrDataItemSelect({
                code: 'GzGroupTypes',
                placeholder: "请选择汇总方式",
                select: function (item) {
                    if (item && item.value != '-1') {
                        page.initHeader();
                        page.search();
                    }
                }
            }).lrselectSet("DepartmentName");

            var myDate = new Date;
            var year = myDate.getFullYear(); //获取当前年
            var mon = myDate.getMonth() + 1; //获取当前月
            $("#monthStart").val(year + "-" + (mon < 10 ? "0" : "") + mon);
            $("#monthEnd").val(year + "-" + (mon < 10 ? "0" : "") + mon);
            $('#company_select').lrselect({
                type: 'tree',
                // placeholder: '请选择机构',
                height: 300,
                maxHeight: 300,
                // 是否允许搜索
                //allowSearch: true,
                select: function (item) {
                    if (item && item.value != '-1') {
                        companyId = item.value;
                        $('#departmentId').lrselectRefresh({
                            // 访问数据接口地址
                            url: config.base_server() + 'departments/tree?access_token=' + config.getToken().access_token,
                            // 访问数据接口参数
                            param: {companyId: item.value, parentId: '0'},
                        });

                        $('#zt_select').lrselectRefresh({
                            // 访问数据接口地址
                            url: config.base_server() + 'gzzts/dropdownlist?access_token=' + config.getToken().access_token,
                            // 访问数据接口参数
                            param: {companyId: companyId},
                            select: function (ztItem) {
                                if (!ztItem || ztItem.value == '-1') {
                                    ztId = "";
                                } else {
                                    ztId = ztItem.value;

                                    page.initHeader();
                                    page.search();
                                }
                            }
                        }).lrselectSet(userdefault.ztId);
                    }
                }
            });
            $('#company_select').lrselectRefresh({
                // 访问数据接口地址
                url: config.base_server() + 'companys/tree?access_token=' + config.getToken().access_token,
                // 访问数据接口参数
                param: {parentId: '0'}
            }).lrselectSet(userdefault.companyId);
            // 帐套选择
            $('#zt_select').lrselect({
                type: 'default',
                //placeholder: '请选择帐套',
                // 展开最大高度
                maxHeight: 300
            });
            // 部门选择
            $('#departmentId').lrselect({
                type: 'tree',
                placeholder: '请选择部门',
                // 是否允许搜索
                allowSearch: true,
                select: function (item) {
                    if (item && item.value != '') {
                        departmentId = item.value;
                    } else {
                        departmentId = '';
                    }
                }
            });

            // 用户类型
            $('#userCategory').lrDataItemSelect({code: 'UserCategorys', placeholder: "请选择"});

            // 查询
            $('#btn_Search').on('click', function () {
                var keyword = $('#txt_Keyword').val();
                page.search({keyword: keyword});
            });
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            //Excel导出
            $("#lr_export").on('click', function () {
                params.access_token = config.getToken().access_token;
                learun.download({
                    url: config.base_server() + 'gzreports/gzhzb/exportExcel',
                    param: params,
                    method: 'GET'
                });
            });
        },
        initGrid: function (headData) {
            $('#gridtable').jfGridClear();
            $('#gridtable').jfGrid({
                url: config.base_server() + 'gzreports/gzhzb/list?access_token=' + config.getToken().access_token,
                headData: headData,
                isPage: true,
                reloadSelected: true,
                mainId: 'code'
            });
        },
        initHeader: function (param) {
            param = param || {};
            param.companyId = companyId;
            param.ztId = ztId;
            param.editable = "0";
            param.groupCode = $('#groupCode').lrselectGet();

            learun.httpPost2(config.base_server() + 'gzreports/gzhzb/header?access_token=' + config.getToken().access_token, param, function (res) {
                learun.loading(false);
                if (res.resp_code == 200) {
                    colNames = res.data.colNames;
                    for (var i = 0; i < res.data.colData.length; i++) {
                        if (res.data.colData[i].formatter) {
                            res.data.colData[i].formatter = eval("(function(){return " + res.data.colData[i].formatter + " })()");
                        }
                        if (res.data.colData[i].edit && res.data.colData[i].edit.change) {
                            res.data.colData[i].edit.change = eval("(function(){return " + res.data.colData[i].edit.change + " })()");
                        }
                        if (res.data.colData[i].children) {
                            for (var j = 0; j < res.data.colData[i].children.length; j++) {
                                if (res.data.colData[i].children[j].formatter) {
                                    res.data.colData[i].children[j].formatter = eval("(function(){return " + res.data.colData[i].children[j].formatter + " })()");
                                }
                            }
                        }
                    }
                    page.initGrid(res.data.colData);
                } else {
                    learun.alert.error(res.resp_msg);
                    learun.httpErrorLog(res.resp_msg);
                }
            });
        },
        search: function (param) {
            param = param || {};
            param.companyId = companyId;
            param.ztId = ztId;
            param.groupCode = $('#groupCode').lrselectGet();
            params = param;

            $('#gridtable').jfGridSet('reload', param);
        }
    };

    // 保存数据后回调刷新
    refreshGirdData = function () {
        page.search();
    };

    page.init();
}


